$(function(){
	$("#menuDetailFrm").validate({
		rules:{
			orderId:{
				digits:true
			}
		},
		errorClass:"has-error",
   		//验证不通过，element：对应验证节点，error：错误信息节点的提示信息
   		errorPlacement: function(error, element) { 
//   		$('#orderIdtd').addClass("has-error");
   			element.addClass("has-error");
   		    $('div.errorInfo').append(error);
   		},
   		//验证通过，label：信息节点的提示信息
   		success:function(label) {
   			label.removeClass("has-error");
   		    $('#orderIdtd').removeClass("has-error");
   		}
	});
});
var prevCurrNode = null;
function menuQueue(){
	this.addNodes = [];
	this.updateNodes = [];
	this.delNodes = [];
};
function menuNode(node){
	this.menuId = node.menuId;
	this.menuName = node.menuName;
	this.parMenuId = node.parMenuId;
	this.url = node.url;
	this.img = node.img;
	this.menuTarget = node.menuTarget;
	this.menuLayer = node.menuLayer;
	this.orderId = node.orderId;
	this.state = node.state;
	this.permission = node.permission;
	this.type = node.type;
}

var operQueue = new menuQueue();
function addQueue(node,type){
	switch(type){
	case 'add':
		operQueue.addNodes.push(new menuNode(node));
		break;
	case 'del':
		var add = true;
		$.each(operQueue.addNodes,function(index,item){
			if(item.menuId == node.menuId){
				operQueue.addNodes.splice(index,1);
				add = false;
			}
		});
		$.each(operQueue.updateNodes,function(index,item){
			if(item.menuId == node.menuId){
				operQueue.updateNodes.splice(index,1);
			}
		});
		if(add){
			operQueue.delNodes.push(new menuNode(node));
		}
		break;
	case 'update':
		var isnew = true;
		$.each(operQueue.updateNodes,function(index,item){
			if(item.menuId == node.menuId){
				item.menuName = node.menuName;
				item.url = node.url;
				item.img = node.img;
				item.orderId = node.orderId;
				item.state = node.state;
				item.permission= node.permission;
				item.type = node.type;
				isnew = false;
				return;
			}
		});
		$.each(operQueue.addNodes,function(index,item){
			if(item.menuId == node.menuId){
				item.menuName = node.menuName;
				item.url = node.url;
				item.img = node.img;
				item.orderId = node.orderId;
				item.state = node.state;
				item.permission= node.permission;
				item.type = node.type;
				isnew = false;
				return;
			}
		});
		if(isnew){
			operQueue.updateNodes.push(new menuNode(node));
		}
	}
}
function savePre(){
	if(prevCurrNode != null){  //保存前一个修改了的节点
		var menuName = $.trim($("#menuDetailFrm input[name='menuName']").val());
		var url = $.trim($("#menuDetailFrm input[name='url']").val());
		var orderId = $.trim($("#menuDetailFrm input[name='orderId']").val());
		var state = $.trim($("#menuDetailFrm select[name='state']").val());
		var permission = $.trim($("#menuDetailFrm input[name='permission']").val());
		var type = $("#menuDetailFrm select[name='type']").val()=="1"?"菜单":"按钮";
		if(menuName == ''){
			menusTree.selectNode(prevCurrNode,false);
			MsgBox.warn("菜单名不能为空","错误提醒",null);
			return false;
		}
		if(orderId != ""){
			if($('#orderIdtd input[name="orderId"]').hasClass("has-error")){
				menusTree.selectNode(prevCurrNode,false);
				MsgBox.warn("同级顺序只能为正整数","错误提醒",null);
				return false;
			}
		}
		if(state == ""){
			state = null;
		}
		if(orderId == ""){
			orderId = null;
		}
		if(url == ""){
			url = null;
		}
		if(permission == ""){
			permission = null;
		}
		//有修改时才添加
		if(prevCurrNode.menuName != menuName || prevCurrNode.url != url
				|| prevCurrNode.orderId != orderId
				|| prevCurrNode.state != state || prevCurrNode.permission != permission
				|| prevCurrNode.type != type){
			prevCurrNode.menuName = menuName;
			prevCurrNode.url = url;
			prevCurrNode.orderId = orderId;
			prevCurrNode.state = state;
			prevCurrNode.permission = permission;
			prevCurrNode.type = type;
			menusTree.updateNode(prevCurrNode,true);
			addQueue(prevCurrNode,'update');
		}
	}
	return true;
}
var tree = {
	nodeClick:function(node){
		if(savePre()){
			if(node.type == "按钮"){
				node.type=0;
			}else{
				node.type=1;
			}
			$("#menuDetailFrm").autofill(node);
			prevCurrNode = menusTree.getCurrentNode();
		}
		$('li>a').prop('href','javascript:void(0);');
		$('li>a').prop('target','');
	}
}
var opreate = {
	fresh:function(){
		$("#menuDetailFrm")[0].reset();
		queryMenusAjax.send();
	},
	add:function(){
		var newNode = menusTree.addNode(menusTree.getCurrentNode());
		if(savePre()){
			$("#menuDetailFrm")[0].reset();
			$("#menuDetailFrm").autofill(newNode);
			prevCurrNode = newNode;
			addQueue(newNode,'add');
		}
	},
	delNode:function(){
		var node = menusTree.getCurrentNode();
		menusTree.removeNode(node);
		$("#menuDetailFrm")[0].reset();
		prevCurrNode = null;
		addQueue(node,'del');
	},
	save:function(){
		if(savePre()){
			saveChangesAjax.addParam("queue",$.toJSON(operQueue));
			saveChangesAjax.send();
		}
	},
	saveBefore:function(){
		$("#msg_dialog_loading").hide();
		$("#orgloading").show();
	},
	saveComplete:function(){
		$("#orgloading").hide();
		operQueue = new menuQueue();//清空队列
	},
	success:function(data){
		if(data.isSuccess == 'true' || data.isSuccess == true){
			MsgBox.info("保存成功！");
		}else{
			MsgBox.warn(data.message,"异常信息",function(){
				opreate.fresh();
			});
		}
	},
	error:function(XMLHttpRequest, textStatus, errorThrown){
		MsgBox.error("保存失败！","信息",function(){
			opreate.fresh();
		});
	},
	onValueChanged:function(ele) {
		var $menuNameInput = $(ele);
		var menuName = $menuNameInput.val();
		var node = menusTree.getCurrentNode();
		if (StringUtil.isBlank(menuName)) {
			MsgBox.warn("菜单名称不能为空", null, function() {
				$menuNameInput.focus();
			});
			return ;
		}
		if(menuName != node.menuName){
			node.menuName = menuName;
			menusTree.updateNode(node,true);
			addQueue(node,'update');
		}
	}
};
var showTr = {
	closeloading:function(){
		$("#orgloading").hide();
		prevCurrNode = null;
		operQueue = new menuQueue();//清空队列
	},
	showloading:function(){
		$("#orgloading").show();
		$("#msg_dialog_loading").hide();
	},
	resetA:function(){
		$('li>a').prop('href','javascript:void(0);');
		$('li>a').prop('target','');
	}
}
